﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Services.Protocols;
using rsdeploygui.ReportService2010;

namespace rsdeploygui
{
    /// <summary>
    /// folder class allows interactions with folders on the report server
    /// </summary>
    class Folder
    {
        private ReportingService2010 rs;

        public Folder (string url)
        {
            try
            {
                rs = new ReportingService2010();
                rs.Credentials = System.Net.CredentialCache.DefaultCredentials;
                rs.Url = url;
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        public ReportService2010.CatalogItem[] ListChildren(string path)
        {
            return this.rs.ListChildren(path, false);
        }

        public ReportService2010.CatalogItem[] ListParents(string path)
        {
            return this.rs.ListParents(path);
        }

        /// <summary>
        /// Create a folder, including all necessary parent folders
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public Boolean Create(string path)
        {
            Boolean success = false;
            Boolean exists = false;
            string folderName = "";
            string parentFolderName = "";

            // get the foldername, not including the parent folder
            if (path.LastIndexOf("/") >= 0)
            {
                folderName = path.Substring(path.LastIndexOf("/") + 1);
            }
            else
            {
                folderName = path;
            }

            // get the parentFolderName, only if there is a parent
            if (path.LastIndexOf("/") > path.IndexOf("/"))
            {
                parentFolderName = path.Substring(0, path.LastIndexOf("/"));
            }

            // if the parentFoldername does not exist, create it
            // recursion is a good thing, woooohooo
            if (!(string.IsNullOrEmpty(parentFolderName)))
            {
                this.Create(parentFolderName);
            }

            // Check if the folder exists, and if it does, set exists to true
            try
            {
                if (rs.GetItemType(path) == "Folder")
                    exists = true;
            }
            catch (SoapException e)
            {
                String sErr = e.Detail.InnerText;
                if (sErr.IndexOf("rsItemNotFound") >= 0)
                {
                    Console.WriteLine("rsItemNotFound: {0}", path);
                    exists = false;
                }
                else if (sErr.IndexOf("rsItemAlreadyExists") >= 0)
                {
                    Console.WriteLine("rsItemAlreadyExists: {0}", path);
                }
                else
                {
                    Console.WriteLine(sErr);
                }
            }

            //If folder does not exist, create it
            if (!(exists))
            {
                try
                {
                    rs.CreateFolder(folderName, parentFolderName, null);
                    Console.WriteLine("Created folder: {0}/{1}", parentFolderName, folderName);
                    success = true;
                }
                catch (SoapException e)
                {
                    Console.WriteLine(e.Detail.InnerText);
                }
            }

            return success;
        }

        public Boolean Delete(string path)
        {
            Boolean success = false;

            try
            {
                rs.DeleteItem(path);
                Console.WriteLine("Deleted folder from dest: {0}", path);
                success = true;
            }
            catch (SoapException e)
            {
                Console.WriteLine(e.Detail.InnerText);
            }

            return success;
        }
        public Boolean Exists(string path)
        {
            Boolean ExistsOut = false;
            if (this.rs.GetItemType(path) == "Folder")
            {
                ExistsOut = true;
            }
            return ExistsOut;
        }
    }
}
